<?php

class IndexController extends Zend_Controller_Action
{
	private $baseURL;

    public function init()
    {
    	$this->baseURL = "http://book.douban.com/";
        /* Initialize action controller here */
    }
	//采集首页的内容
    public function indexAction()
    {
		$cnt = $this->geturl($this->baseURL);
		
		$preg_hot_tag = '/<ul class="hot-tags-col5">(.*?)<h2>/is';
		preg_match($preg_hot_tag,$cnt,$hot_tag_cnt);
    	$hot_tag_cnt[1] = preg_replace("/<a.*?>\(更多\)<\/a>/", '', $hot_tag_cnt[1]);
    	$preg_hot_tag_item = '/<a .*?>(.*?)<\/a>/is';
    	preg_match_all($preg_hot_tag_item,$hot_tag_cnt[1],$hot_tag);
	
    	$this->view->taglist = $hot_tag[1];			//热门tag标签
    	$preg_new_tag = '/<div class="detail-frame">.*?<h2>(.*?)<\/h2>.*?<p class="color-gray">(.*?)<\/p>.*?<p>(.*?)<\/p>.*?href="http:\/\/book\.douban\.com\/subject\/(\d+?)\/".*?<\/li>/is';
    	preg_match_all($preg_new_tag, $cnt, $new_tag);
		for($i=0;$i!=count($new_tag[1]);$i++)
		{
			$newlist[$i] = array('url'=>$new_tag[4][$i],'title'=>$new_tag[1][$i],'description'=>$new_tag[3][$i],'info'=>$new_tag[2][$i]);
		}
		$this->view->newlist = $newlist;		//最新阅读标签
		//$preg_guanzhu = '/<li class="clearfix">.*<h2>.*<a .*href=".*\/(\d+)\/">(.*?)<\/a>.*<p class="pt6">(.*?)(<a.*href=".*\/(\d+)\/"/is';
		$preg_guanzhu = '/<li class="clearfix">.*?<h2>.*?<a .*?href=".*?\/(\d+)\/">(.*?)<\/a>.*?<\/h2>.*?color-red fleft">(.*?)<\/span>.*?<span class="fleft ml8">(.*?)<\/span>.*?<br \/>(.*?)<\/p>.*?<p class="pt6">(.*?)\(<a.*?href=".*?\/(\d+)\/">(.*?)评论<\/a>/is';
		preg_match_all($preg_guanzhu,$cnt,$guanzhu);

    	for($i=0;$i!=count($guanzhu[1]);$i++)
    	{
    		$guanzhu_list[$i]= array(
    				'url'=>$guanzhu[1][$i],				//url id
    				'title'=>$guanzhu[2][$i],			//标题
    				'hot'=>$guanzhu[3][$i],				//热门程度
    				'shangbang'=>$guanzhu[4][$i],			//上榜类型
    				'info'=>trim(str_replace('<br />','',$guanzhu[5][$i])),				//相关信息
    				'description'=> $guanzhu[6][$i],			//描述
    				'pl_id'	=>	$guanzhu[7][$i],			//评论id
    				'pl_name'	=>$guanzhu[8][$i]			//评论者
    				);
    	}
    	$this->view->guanzhu = $guanzhu_list;		//最新关注标签
    	
    	
    	$preg_top520 = '/<dd><a.*?(\d+)\/">(.*?)<\/a>/is';
    	preg_match_all($preg_top520,$cnt, $_top520_list);
    	for($i=0;$i!=count($_top520_list[1]);$i++)
    	{
    		$top520[$i]=array(
    		'title'=>$_top520_list[2][$i],
    		'url'=>$_top520_list[1][$i]
    				);
    	}
    	$this->view->top520 = $top520;
    }

	public function listAction()
	{
		$service = $this->getRequest()->getParams();
		$param = split('_',$service['id']);
		$url = $this->baseURL.'tag/'.$param[0];
		$url .= isset($param[1]) ? '?start='.$param[1].'&type=T' : '';
		
		$cnt = $this->geturl($url);
		
		/**具体列表的内容**/
		$preg_list_array = '/<tr class="item">.*?href="http:\/\/book.douban.com\/subject\/(.*?)\/".*?title="(.*?)".*?src="(.*?)".*?<p class="pl">(.*?)<\/p>.*?<span class="rating_nums">(.*?)<\/span><span class="pl">\((.*?)\)<\/span>/is';
		preg_match_all($preg_list_array, $cnt, $list);
		for($i=0;$i!=count($list[1]);++$i)
		{
			$listArray[$i]=array(
					'url'=>$list[1][$i],
					'title'=>$list[2][$i],
					'imgurl' => $list[3][$i],
					'info' => $list[4][$i],
					'hot'=>$list[5][$i],
					'pl' =>$list[6][$i]
					);
		}
		$this->view->listArray = $listArray;
		
		
		/**翻页显示内容**/
		$preg_page = '/<div class="paginator">(.*?)<\/div>/is';
		preg_match($preg_page,$cnt,$page_);
		$page = str_replace('http://book.douban.com/tag', '/list', $page_[1]);
		$page = str_replace('?start=','_',$page);
		$page = str_replace('&type=T','',$page);
		$this->view->page = $page;
		
		/**相关标签**/
		$preg_xiangguan = '/<div class="tags-list">(.*?)<\/div>/is';
		$preg_xg2 = '/">(.*?)<\/a>/is';
		preg_match($preg_xiangguan,$cnt, $_xiangguan);
		preg_match_all($preg_xg2,$_xiangguan[1],$_xglist);
		$this->view->xiangguan = $_xglist[1];	
	}
	
	/**
	 * 具体文章页面 
	 */
	
	public function viewAction()
	{
		$urlparam = $this->getRequest()->getParams();
		$url = $this->baseURL.'subject/'.$urlparam['id'].'/';
		$cnt = $this->geturl($url);
		$this->view->id = $urlparam['id'];
		
		
		//相关信息一
		$preg_info1 = '/<span property="v:itemreviewed">(.*?)<\/span>.*?<img src="(.*?)".*?<div id="info">(.*?)<\/div>.*?property="v:average">(.*?)<\/strong>.*? property="v:votes">(.*?)<\/span>/is';
		$preg_info2 = '/class="indent">(.*?)<\/div>/is';
		//获取相关信息一
		preg_match($preg_info1,$cnt,$_cnt);
		//获取相关信息二
		preg_match_all($preg_info2,$cnt,$_cnt2);
		$_cnt2 = $_cnt2[1];
		
		//分析内容相关，显示出所有的内容
		if(preg_match('/<span class="all hidden">(.*?)<\/span>/is', $_cnt2[1],$_cnt2_allcnt))
		{
			$_cnt2[1] = $_cnt2_allcnt[1]; 
		}
		//分析常用标签
		$const_tag = $_cnt2[3];
		$const_tag = str_replace('http://book.douban.com/tag/','/list/', $const_tag);
		$_cnt[2] = preg_replace("/<a[^>]*>/i", "", $_cnt[2]);
		$_cnt[2] = preg_replace("/<\/a>/i", "", $_cnt[2]);
		$content = array(
			'title'=>$_cnt[1],
			'imgurl'=>$_cnt[2],
			'info'=>$_cnt[3],
			'hot'=>$_cnt[4],
			'pl_cnt'=>$_cnt[5],
			'cnt_info'=>$_cnt2[1],
			'auth_info'=>$_cnt2[2],
			'const_tag' =>$const_tag,
		);
		
		
		
		$this->view->content = $content;
		
		//书评
		$preg_shuping = '/<ul class="tlst clearfix" style="clear:both">.*?href="http:\/\/book.douban.com\/review\/(\d+)\/".*?>(.*?)<\/a>.*?<span class="starb"> <a href="http:\/\/book.douban.com\/people.*?>(.*?)<\/a>.*?short\'>(.*?)<div class="pl clearfix">/is';
		preg_match_all($preg_shuping, $cnt,$shuping_);
		str_replace('http://book.douban.com','',$shuping_);
		for($i=0;$i!=count($shuping_);++$i)
		{
			$shuping_list[$i] = array(
				'url'=>$shuping_[1][$i],
				'title'=>$shuping_[2][$i],
				'name'=>$shuping_[3][$i],
				'cnt'=>$shuping_[4][$i]		
			);
		}
		$this->view->shuping = $shuping_list;
		
		
		//读书笔记
		$preg_biji = '/<div class="con">.*?<a href="http:\/\/book.douban.com\/annotation\/(\d+)\/">(.*?)<\/a>.*?<a.*?>(.*?)<\/a>(.*?)<\/p>.*?<blockquote><q>(.*?)<\/q>/is';
		preg_match_all($preg_biji, $cnt, $biji_pregcnt);
		
		for($i=0;$i!=count($biji_pregcnt[1]);++$i)
		{
			$biji[$i]= array(
				'url'=>$biji_pregcnt[1][$i],
				'title'=>$biji_pregcnt[2][$i],
				'name'=>$biji_pregcnt[3][$i],
				'notice'=>trim($biji_pregcnt[4][$i]),
				'cnt'=>$biji_pregcnt[5][$i]
			);
		}
		$this->view->biji = $biji;
	}
	
	/**
	 * 评论
	 */
	
	public function commentAction()
	{
		$param = $this->getRequest()->getParams();
		$param = split('_',$param['id']);
		$url = $this->baseURL.'subject/'.$param['id'].'/collections';
		$url .= isset($param[1]) ? '?start='.$param[1] : '';
		$url2 = $this->baseURL.'subject/'.$param['id'];
		$cnt = $this->geturl($url,$url2);
		//评论列表
		$preg_list = '/<td valign="top">(.*?)<\/td>/is';
		preg_match_all($preg_list, $cnt, $preg_listArray);
		var_dump($preg_listArray,$cnt);
		
		
		
	}
	protected function geturl($url,$refer = null)
	{
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch,CURLOPT_USERAGENT,"baiduspider+(+http://www.baidu.com/search/spider.htm)");
		curl_setopt($ch, CURLOPT_AUTOREFERER,true);
	//	curl_setopt ($ch, CURLOPT_REFERER,$refer);
		
		$cnt = curl_exec($ch);
		curl_close($ch);
		return $cnt;
	}
}

